//配置axios的全局基本路径
axios.defaults.baseURL='http://localhost:8080/';
//axios.defaults.baseURL='/api' //前端配置代理，解决跨域问题
//==============配置axios前置拦截器=======================
axios.interceptors.request.use(config=>{
    let  token = localStorage.getItem("token");
    if(token){
        //请求头中设置token
        config.headers['token'] = token;
    }
    return config;
});
//====================配置axios前置拦截器==================
//=============配置axios前置拦截器,拦截报错，跳转到登录页=============
axios.interceptors.response.use(config=>{
    let data = config.data;
    if(!data.success && data.message === "noLogin"){
        localStorage.removeItem("token");
        localStorage.removeItem("logininfo");
        location.href="login.html";
    }
    return config;
});
//============================

//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios;

//前端资源拦截：实现思路，只需要判断localStorage中有没有登录信息即可
let url = location.href;//得到请求路径
//如果是不是登录，且也不是注册，需要判断权限
if(url.indexOf("login.html") == -1 && url.indexOf("register.html") == -1 && url.indexOf("callback.html") == -1 && url.indexOf("binder.html") == -1){
    let logininfo = localStorage.getItem("logininfo");
    if(!logininfo){
        location.href = "login.html";
    }
}

/**
 * 解析url获取参数对象
 * @param url
 * @returns {{}}
 */
function parseUrlParams2Obj(url){
    //获取请求参数
    let paramStr = url.substring(url.indexOf("?")+1);//code=021FdXFa14mYlA016OGa1L3R4X0FdXFO&state=1
    let paramStrs = paramStr.split("&");//[code=021FdXFa14mYlA016OGa1L3R4X0FdXFO,state=1]
    let paramObj = {};
    for(let i=0;i<paramStrs.length;i++){
        let paramName = paramStrs[i].split("=")[0];//code
        let paramValue = paramStrs[i].split("=")[1];//021FdXFa14mYlA016OGa1L3R4X0FdXFO
        paramObj[paramName] = paramValue;
    }
    return paramObj;
}
